package com.creative.accountsdk.holder;

import android.app.Dialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.creative.accountsdk.AccountSdk;
import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.OnLoginListener;
import com.creative.accountsdk.R;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.CheckTeacherInviteCodeProtocol;
import com.creative.accountsdk.net.account.TeacherInviteProtocol;
import cz.msebera.android.httpclient.Header;
import org.json.JSONObject;

/**
 * Created by wang_qian on 2015/11/24.
 */
public class TeacherInviteCodeHolder extends BaseHolder {

    int currentRole;
    private Button btnNext;
    private EditText  etInviteCode,etTeacherName;
    private TextView  tvCheck;
    private TextView  tvSchookInfo;

    public  TeacherInviteCodeHolder(int currentRole){
          this.currentRole = currentRole;
    }
    @Override
    protected void initViews() {
        btnNext = (Button)viewRoot.findViewById(R.id.btn_invitecode_next);
        etInviteCode = (EditText) viewRoot.findViewById(R.id.et_invite_code);
        etTeacherName = (EditText)viewRoot.findViewById(R.id.et_teacher_name);
        tvCheck = (TextView)viewRoot.findViewById(R.id.tvCheck);
        tvSchookInfo = (TextView)viewRoot.findViewById(R.id.tvSchoolName);

        setLister();
}

    private void  setLister(){
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendNet();
            }
        });
        tvCheck.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(etInviteCode.getEditableText().toString())) {
                    checkTeacherInviteCode();
                }
            }
        });
    }

    private void sendNet(){
        progress = DialogUtils.createProgress(activity);
        progress.show();
        final TeacherInviteProtocol protocol = new TeacherInviteProtocol(currentRole,
                etInviteCode.getText().toString(),
                etTeacherName.getText().toString());

        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                Log.d("TAG,", "response....." + response.toString());
                protocol.debugUrl(DataCenter.getHttpClient());
                if (protocol.parse(response, activity)) {
                  /*  OnLoginListener loginListener = AccountSdk.getInstance().getLoginListener();
                    if (loginListener != null) {
                        loginListener.onComplete(protocol.jsonUser);
                    }*/
                    toUserInfor();
                }

            }
        });
    }
    private void  toUserInfor(){
        activity.hideHolder();
        activity.userInfoHolder = new UserInfoHolder(activity);
        activity.showHolder(activity.userInfoHolder);
    }

    @Override
    public int getLayoutResID() {
        return R.layout.teacher_invitecode_layout;
    }

    private void checkTeacherInviteCode() {

        progress = DialogUtils.createProgress(activity);
        progress.show();
        final CheckTeacherInviteCodeProtocol protocol = new CheckTeacherInviteCodeProtocol(etInviteCode.getText().toString());

        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().get(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, activity)) {
                    if (!TextUtils.isEmpty(protocol.schoolName)) {
                        tvSchookInfo.setVisibility(View.VISIBLE);
                        tvSchookInfo.setText(activity.getResources().getString(R.string.school_name) + protocol.schoolName);
                    }
                }
            }
        });
    }
}
